package za.co.chris.traintimes;

import java.util.ArrayList;
import java.util.List;

import za.co.chris.traintimes.beans.Constants;
import za.co.chris.traintimes.beans.ResultLayout;
import za.co.chris.traintimes.beans.TimeBean;
import za.co.chris.traintimes.database.DatabaseHelper;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class TimeResults extends Activity {

	private String fromStation;
	private String toStation;
	private int fromPosition;
	private int toPosition;
	private String fromLine;
	private String toLine;
	private String interchange;
	private String selectedTime;
	private String dayOfWeek;
	private DatabaseHelper helper;
	private List<ResultLayout> layouts;
	private LinearLayout layout;
	LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 1);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_time_results);
		helper = DatabaseHelper.getInstance(this);
		layout = (LinearLayout) findViewById(R.id.layout);
		fromStation = this.getIntent().getStringExtra(Constants.FROM_STATION_KEY);
		toStation = this.getIntent().getStringExtra(Constants.TO_STATION_KEY);
		fromPosition = this.getIntent().getIntExtra(Constants.FROM_STATION_POSITION_KEY, 1);
		toPosition = this.getIntent().getIntExtra(Constants.TO_STATION_POSITION_KEY, 1);
		fromLine = this.getIntent().getStringExtra(Constants.FROM_LINE_KEY);
		toLine = this.getIntent().getStringExtra(Constants.TO_LINE_KEY);
		selectedTime = this.getIntent().getStringExtra(Constants.SELECTED_TIME_KEY);
		dayOfWeek = this.getIntent().getStringExtra(Constants.DAY_SELECTED_KEY);
		interchange = this.getIntent().getStringExtra(Constants.INTERCHANGE_STATION_KEY);
		layouts = getResults(fromStation, toStation, interchange, fromLine, toLine, selectedTime, dayOfWeek);
		layout.setWeightSum(2);
		for (ResultLayout time : layouts) {
			time.setLayoutParams(params);
			layout.addView(time);
			layout.addView(getSpacer());
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		// for (ResultLayout time : layouts) {
		// time.setLayoutParams(params);
		// layout.addView(time);
		// layout.addView(getSpacer());
		// }
	}

	private View getSpacer() {
		View spacer = new View(this);
		spacer.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
		spacer.setBackgroundColor(Color.DKGRAY);
		return spacer;
	}

	private List<ResultLayout> getResults(String fromStationId, String toStationId, String interchangeId, String fromLineId, String toLineId, String selectedTime, String dayOfWeek) {
		List<ResultLayout> times = new ArrayList<ResultLayout>();

		if (interchangeId == null) {
			String direction = (fromPosition < toPosition) ? "out" : "in";
			TimeBean bean = helper.getAndWrapStations(fromStationId, toStationId, fromLineId, direction, selectedTime, dayOfWeek);

			times.add(makeLayout(bean));
		} else {
			TimeBean bean = helper.getAndWrapStations(fromStationId, interchangeId, fromLineId, "in", selectedTime, dayOfWeek);
			times.add(makeLayout(bean));
			if (bean != null) {
				if (bean.getTimeElements().size() != 0) {
					selectedTime = bean.getTimeElements().get(0).endTime;
				}
			}

			bean = helper.getAndWrapStations(interchangeId, toStationId, toLineId, "out", selectedTime, dayOfWeek);
			times.add(makeLayout(bean));
		}

		return times;
	}

	private ResultLayout makeLayout(TimeBean bean) {
		ResultLayout layout = new ResultLayout(this.getBaseContext(), bean.getFromStation(), bean.getToStation(), bean.getTimeElements());
		return layout;
	}
}
